const app = getApp();

Page({
	data:{
		url:'../Ranking/list',
		reward_content:'',
		exp_data:[
				{"name":"[微笑]","src":"1"},
				{"name":"[撇嘴]","src":"2"},
				{"name":"[好色]","src":"3"},
				{"name":"[发呆]","src":"4"},
				{"name":"[得意]","src":"5"},
				{"name":"[流泪]","src":"6"},
				{"name":"[害羞]","src":"7"},
				{"name":"[闭嘴]","src":"8"},
				{"name":"[睡觉]","src":"9"},
				{"name":"[大哭]","src":"10"},
				{"name":"[尴尬]","src":"11"},
				{"name":"[发怒]","src":"12"},
				{"name":"[调皮]","src":"13"},
				{"name":"[呲牙]","src":"15"},
				{"name":"[惊讶]","src":"14"},
				{"name":"[难过]","src":"16"},
				{"name":"[酷酷]","src":"17"},
				{"name":"[抓狂]","src":"18"},
				{"name":"[冷汗]","src":"19"},
				{"name":"[狂吐]","src":"20"},
				{"name":"[偷笑]","src":"21"},
				{"name":"[装傻]","src":"25"},
				{"name":"[傲慢]","src":"23"},
				{"name":"[饥饿]","src":"24"},
				{"name":"[坏笑]","src":"25"},
				{"name":"[再见]","src":"26"},
				{"name":"[委屈]","src":"27"},
				{"name":"[吓到]","src":"28"},
				{"name":"[头晕]","src":"29"},
				{"name":"[小声]","src":"30"},
				{"name":"[快哭]","src":"31"},
				{"name":"[发困]","src":"32"},
				{"name":"[憨笑]","src":"33"},
				{"name":"[亲亲]","src":"35"},
				{"name":"[大骂]","src":"34"},
				{"name":"[可爱]","src":"36"},
				{"name":"[哼哼]","src":"38"},
				{"name":"[鄙视]","src":"37"},
				{"name":"[疑惑]","src":"39"},
				{"name":"[很衰]","src":"40"},
				{"name":"[抠鼻]","src":"41"},
				{"name":"[奋斗]","src":"42"},
				{"name":"[擦汗]","src":"43"},
				{"name":"[哈欠]","src":"44"},
				{"name":"[阴险]","src":"45"},
				{"name":"[惊恐]","src":"46"},
				{"name":"[鼓掌]","src":"47"},
				{"name":"[醜大]","src":"49"},
				{"name":"[大兵]","src":"48"},
				{"name":"[流汗]","src":"50"},
				{"name":"[敲打]","src":"52"},
				{"name":"[蜡烛]","src":"51"},
				{"name":"[蛋糕]","src":"53"},
				{"name":"[爱心]","src":"54"},
				{"name":"[玫瑰]","src":"55"},
				{"name":"[凋零]","src":"56"},
				{"name":"[气球]","src":"57"},
				{"name":"[熊猫]","src":"58"},
				{"name":"[药丸]","src":"59"},
				{"name":"[双喜]","src":"61"},
				{"name":"[嘴唇]","src":"60"},
				{"name":"[灯笼]","src":"62"},
				{"name":"[礼物]","src":"63"},
				{"name":"[棒棒糖]","src":"64"},
				{"name":"[鞭炮]","src":"65"},
				{"name":"[西瓜]","src":"66"},
				{"name":"[吃饭]","src":"67"},
				{"name":"[太阳]","src":"68"},
				{"name":"[月亮]","src":"69"},
				{"name":"[电灯]","src":"70"},
				{"name":"[抱抱]","src":"71"},
				{"name":"[猪头]","src":"72"},
				{"name":"[奶瓶]","src":"73"},
				{"name":"[礼炮]","src":"74"},
				{"name":"[抱拳]","src":"75"},
				{"name":"[yes]","src":"76"},
				{"name":"[no]","src":"77"},
				{"name":"[ok]","src":"78"},
				{"name":"[点赞]","src":"79"},
				{"name":"[握手]","src":"80"},
				{"name":"[拳头]","src":"81"},
				{"name":"[咖啡]","src":"82"},
				{"name":"[纸巾]","src":"83"},
				{"name":"[发财]","src":"84"},
				{"name":"[啤酒]","src":"85"},
				{"name":"[钞票]","src":"86"},
				{"name":"[闪电]","src":"87"},
				{"name":"[篮球]","src":"88"},
				{"name":"[香蕉]","src":"89"},
				{"name":"[下雨]","src":"90"},
				{"name":"[吃面]","src":"91"},
				{"name":"[钻戒]","src":"92"}
			],
			can_list:[],
      set_font: true
	},
	canvasIdErrorCallback(e) {
    console.error(e.detail.errMsg)
  },
  onReady(e) {
    app.SystemInfo(this)
		console.log('开始绘画')
		var that = this;
		//画布  
		app.request(app.apiDomain + 'index.php?c=user&a=stepStat', {}, function (res) {
				//一周步数
				var z_step = 0
				for(var i=0; i<7 ;i++){
					z_step = z_step + parseFloat(res.data.data[i].step)
				}
				var r_step = res.data.data[6].step;
				//
				that.setData({
						can_list:res.data.data,
						step_num:r_step,
						ka_num:(r_step/20).toFixed(1),
						z_step_num:z_step,
						z_ka_num:(z_step/20).toFixed(1),
						ka_tang:(r_step/20/3.89).toFixed(1),
						ka_zhi:(r_step/20/9).toFixed(1),
						ka_qiao:(r_step/20/5.5).toFixed(1),
				});
				
				console.log(that.data)
				
				// console.log(res.data.data)
				const context = wx.createCanvasContext('per_canvas');
				// const can_list = res.data.data;
				//处理
				wx.getSystemInfo({
						success(res) {
								var width = res.windowWidth*0.76;
								var height = width*0.4;
								var a_win = width/6;
								var a_hei = height/22000;
								var h_hie = height*0.97;
								// 
								var data=that.data.can_list;
								context.lineWidth = 2;//线条的宽度
								context.strokeStyle = "#ffef87";//线条的颜色
                console.log(data)
								for(var i in data){
										if(parseFloat(data[i].step) < 20000){
                      var a = h_hie - (data[i].step * a_hei)
										}else{
											 var a = 5
										}
										context.lineTo(parseFloat(a_win*i)+(width/40),a);
								}
								context.stroke();
								
								//画圆
								for(var i in data){
										if(parseFloat(data[i].step) < 20000){
											var a = h_hie-(data[i].step * a_hei)
										}else{
											var a = 5
										}
										context.fillStyle="#FEDC01";
										context.beginPath();
										context.arc(parseFloat(a_win*i)+(width/40),a,3.5,0,Math.PI*2,true);
										context.closePath();
										context.fill();
								}
                setTimeout(() => {
                  context.draw()
                }, 1000)
								
								
						}
				})
		});
  },
	onLoad:function(e){
		var that = this;
    that.setData({
      isGet: wx.getStorageSync('isGetChicken')
    })
    console.log(wx.getStorageSync('isGetChicken'))
		//推荐宝贝
// 		app.request(app.apiDomain + 'index.php?c=mall&a=queryRecommend', {
// 			 size:2
// 		}, function (res) {
// 			// console.log(res)
// 			   that.setData({
// 						goods_list:res.data.data
// 				 })
// 		});
		//判断那里过来的
		if(e.type=='pres'){
			this.setData({
				url:'../personal/index',
			});
		};
		if(e.type=='index'){
			this.setData({
				url:'../index/index',
			});
		};
		//表情
		
		//
		// console.log(cover_img)
    this.setData({
      userInfo: app.userInfo,
      cover_img: wx.getStorageSync('cover_img')
    });
    // if (!app.userInfo.nickname || !app.userInfo.icon) {
			  //获取头像名字信息
			  // app.userstat(this);
    // }

    app.request(app.apiDomain + 'index.php?c=point&a=account', {}, function (res) {
			console.log('获取头像名字信息',res.data)
      if (res.data.status == '1') {
        let step_count = parseInt(parseFloat(res.data.data.step_count)/10000)
        that.setData({
          userInfo: res.data.data,
					cover_img:res.data.data.cover_img,
          step_count: step_count
        });
      }
    });
    
    app.request(app.apiDomain + 'index.php?c=point&a=latestreward', {}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward: res.data.data,
          reward_likes: res.data.data ? res.data.data.likes : {},
          reward_like_num: res.data.data ? res.data.data.like_num : 0,
          reward_like: res.data.data ? res.data.data.reward_like : false,
					// reward_content:that.convert(res.data.data.content)
        });
      }
			
			
    });
    
    app.request(app.apiDomain + 'index.php?c=user&a=latestdaily', {}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          daily: res.data.data
        });
      }
    });

    app.request(app.apiDomain + 'index.php?c=point&a=follows', {}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          follow: res.data.data.list
        });
      }
    });
	},
	formSubmit: function(e) {
			console.log(e.detail);
			var ind = e.detail.target.dataset.index;
			var zan_id = e.detail.target.dataset.id;
			var num = parseFloat(e.detail.target.dataset.num) + 1
			this.setData({
				['follow[' + ind + '].liked']: 'true',
				['follow[' + ind + '].like_num']: num
					
			});
			//发给服务器
			app.request(app.apiDomain + 'index.php?c=user&a=like', {
				// user
				id: zan_id,
				type: 'day_step',
				// form_id: e.detail.formId,
        // back: 1
			}, function(res) {
				console.log(res.data.info)
			});
			const innerAudioContext = wx.createInnerAudioContext('zan')
			innerAudioContext.autoplay = true 
			innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/img/zou_lu/zan.mp3'
			innerAudioContext.onPlay(() => {
					console.log('开始播放')
			});
	},
  return_act() {
    wx.redirectTo({
      url: '../index/index',
    })
  },
  onUpload: function () {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.apiDomain + 'index.php?c=user&a=upload', // 仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'image',
          formData: {
            user: 'test'
          },
          success(res) {
            var data = JSON.parse(res.data);
            // console.log(app.resourceDomain + data.data);
            
            app.request(app.apiDomain + 'index.php?c=user&a=setcoverimg', {cover_img: app.resourceDomain + data.data}, function              (res) {
                if (res.data.status == '1') {
                    that.setData({
                      cover_img: app.resourceDomain + data.data
                    });
                     wx.setStorageSync('cover_img', app.resourceDomain + data.data);
                }
            });
            // do something
          },
          fail: function (res) {
            app.request(app.apiDomain + "index.php?c=user&a=uploadFail&type=coverimageupload&res=" + JSON.stringify(res));
          }
        })
      },
      fail: function (res) {
        app.request(app.apiDomain + "index.php?c=user&a=uploadFail&type=coverimagechoose&res=" + JSON.stringify(res));
      }
    })
  },
  like: function (e) {
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=like&type=reward&id=' + e.target.dataset.id, {}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward_like: 1,
          reward_like_num: res.data.data.like_num,
          reward_likes: res.data.data.like_users
        });
      }
    });
  },
  unlike: function (e) {
    var that = this;
    app.request(app.apiDomain + 'index.php?c=user&a=unlike&type=reward&id=' + e.target.dataset.id, {}, function (res) {
      if (res.data.status == '1') {
        that.setData({
          reward_like: 0,
          reward_like_num: res.data.data.like_num,
          reward_likes: res.data.data.like_users
        });
      }
    });
  },
  morelike: function () {
    wx.navigateTo({
      url: 'praised',
    })
  },
  onShareAppMessage: function (res) {
    let that = this
    if (res.target.id == 1) {
      return app.onShareAppMessage();
    } else {
      that.setData({
        set_font: true,
        qian_on: '',
        qian_type: ''
      })
      return {
        path: '/pages/index/index?user_id=' + that.data.user_id + '&rouse_user_id=' + that.data.user_id,
        title: '我给您送了' + that.data.money + '个健康金币,快来领取吧！',
        imageUrl: app.resourceDomain + 'canvas/healthy/img/badge/share_badge.jpg'
      }
    }
    
  } ,
	convert:function(words){
			//自定义hashtable
			function Hashtable() {
					this._hash = new Object();
					this.put = function(key, value) {
							if (typeof (key) != "undefined") {
									if (this.containsKey(key) == false) {
											this._hash[key] = typeof (value) == "undefined" ? null : value;
											return true;
									} else {
											return false;
									}
							} else {
									return false;
							}
					}
					this.remove = function(key) { delete this._hash[key]; }
					this.size = function() { var i = 0; for (var k in this._hash) { i++; } return i; }
					this.get = function(key) { return this._hash[key]; }
					this.containsKey = function(key) { return typeof (this._hash[key]) != "undefined"; }
					this.clear = function() { for (var k in this._hash) { delete this._hash[k]; } }
			}
			var img_list = new Hashtable();
			for(i in this.exp_data){
				img_list.put(this.exp_data[i].name, this.exp_data[i].src);
			};
			if(typeof (words) != "undefined") {
					var wrds = words.match(/\[.*?\]/g);
					if(null!=wrds && '' != wrds){
							for(var i = 0; i < wrds.length; i++){
									var html = '<image mode="aspectFill" src="//resource.160nurse.com/canvas/healthy/img/Extracted/'+img_list.get(wrds[i])+'.gif"></image>';
									words = words.replace(wrds[i], html);
							}
					}
			}
			return words;
	},
  // 送金币
  sendGod: function (e) {
    let id = e.currentTarget.dataset.id
    console.log(id)
    this.setData({
      qian_on: 'on',
      user_id: id
    })
  },
  // 关闭送金币
  qian_off: function () {
    this.setData({
      qian_on: '',
    })
  },
  qian_click: function (e) {
    let that = this
    if (that.data.set_font) {
      var a = e.currentTarget.dataset.id;
      this.qian_num = a >= 0 ? a : 20;
      if (a == 'a') {
      this.qiam_type = '2'
      } else if (a == 'b') {
      this.qiam_type = '3'
      } else if (a == 'c') {
      this.qiam_type = '4'
      } else {
      this.qiam_type = '1'
      }
      console.log(a, this.qiam_type)
      this.setData({
      qian_type: 'type_' + e.currentTarget.dataset.id
      })
    }
  },
  // 选择金币发送 
  qian_submit() {
    let that = this
    if (that.data.qian_type) {
      app.request(app.apiDomain + 'index.php?c=user&a=shang', {
        user_id: that.data.user_id,
        money: that.qian_num
      }, function (res) {
        if (res.data.status == 1) {
          wx.showToast({
            title: '金币打赏成功，去通知好友吧！',
            icon: 'none',
            duration: 1000
          })
          // console.log('发送成功')
          // that.getData()
          app.request(app.apiDomain + 'index.php?c=point&a=follows', {}, function (res) {
            if (res.data.status == '1') {
              that.setData({
                follow: res.data.data.list,
                set_font: false,
                money: that.qian_num
              });
            }
          });
          // for (let item of that.data.list) {
          //   if (item.id == that.data.user_id) {
          //     item.shang = true
          //     item.money = that.qian_num
          //   }
          // }
          that.setData({
            // qian_on: '',
            // qian_type: ''
          })
        } else {
          wx.showToast({
            title: res.data.info,
            icon: 'none',
            duration: 1000
          })
          that.setData({
            qian_on: '',
            qian_type: ''
          })
        }
      })
    } else {
      wx.showToast({
        title: '请选择金币金额',
        icon: 'none',
        duration: 1000
      })
    }
  },
})



